package com.example.yangxu0703.thundermapclient.fragment;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.yangxu0703.thundermapclient.R;
import com.example.yangxu0703.thundermapclient.activity.PreViewActivity;
import com.example.yangxu0703.thundermapclient.adapter.VedioAdapter;
import com.example.yangxu0703.thundermapclient.iterface.VEDCallback;
import com.example.yangxu0703.thundermapclient.thread.FileDeleteThread;
import com.example.yangxu0703.thundermapclient.thread.SearcherThread;

import com.thundersoft.mm.phone.Wifi.WifiP2pSocket;
import com.truizlop.sectionedrecyclerview.SectionedSpanSizeLookup;

import java.io.File;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;


public class VedioFragment extends Fragment {

    private boolean chooseMode;
    private View Contecntview;
    private RecyclerView recyclerView;
    public static ArrayList<ArrayList<String>> listVedio;
    private VedioAdapter adapter;
    private ProgressDialog dialog;
    private TextView action;
    private boolean [][] vedio_staus;
    public  ArrayList<String> deleteList;
    @SuppressLint("HandlerLeak")
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (listVedio.size()!=0){
                        action.setVisibility(View.GONE);
                        recyclerView.setVisibility(View.VISIBLE);
                    }else {
                        recyclerView.setVisibility(View.GONE);
                        action.setVisibility(View.VISIBLE);
                        action.setText("该目录下无图片文件！");
                    }
                    initAdapter();
                    Log.i("VedioFragment","initAdapter"+adapter);
                    break;
                case 2:
                    if (adapter==null){
                        initAdapter();
                    }
                    adapter.setMode(false);
                    adapter.setList(listVedio);
                    adapter.notifyDataSetChanged();
                    dismiss();
                    break;
                case 3:
                    /*HoldByDialog.getinstance(getActivity());
                    HoldByDialog.showDialog("正在删除..");*/
                    showDialog("正在删除..");
                    notifydata();
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Contecntview = inflater.inflate(R.layout.fragment_vedio, container, false);
        recyclerView = Contecntview.findViewById(R.id.vedio_list);
        //initAdapter();
        action=Contecntview.findViewById(R.id.notifaction_vedio);
        deleteList = new ArrayList<>();
        vedio_staus=new boolean[100][100];
        SearcherThread threadVdeio = new SearcherThread(WifiP2pSocket.path, 2);
        threadVdeio.setVedioCallback(new VEDCallback() {

            @Override
            public void sortVedioBcak(ArrayList<ArrayList<String>> url) {
                Log.i("VedioFragment","url");
                listVedio=url;
                Log.i("VedioFragment","url"+url);
                handler.sendEmptyMessage(1);
            }

        });
        threadVdeio.start();
        dialog=new ProgressDialog(getActivity());
        Log.i("VedioFragment","onCreateView");
        return Contecntview;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("VedioFragment","onResume");

    }

    public void setChooseMode(boolean chooseMode) {
        if (adapter==null) {
            notifydata();
        }
        updateStatus();
        deleteList=new ArrayList<>();
        this.chooseMode = chooseMode;
        Log.i("VedioFragment","setChooseMode");
        adapter.setMode(chooseMode);
        adapter.setVedioDate(listVedio,vedio_staus);
        adapter.notifyDataSetChanged();
    }

    public void deletefile() {
        //delete file
        FileDeleteThread thread =new FileDeleteThread(deleteList);
        Log.i("VedioFragment",deleteList+"delete size");
        thread.setFinishCallback(new FileDeleteThread.FinishCallback() {
            @Override
            public void finishback(boolean status) {
                if (status){
                    handler.sendEmptyMessage(3);
                    deleteList.clear();
                }
            }
        });
        thread.start();

    }


    private void initAdapter() {
        if (listVedio!=null){
            updateStatus();
            adapter =new VedioAdapter(listVedio,vedio_staus,getActivity());
            GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
            SectionedSpanSizeLookup lookup = new SectionedSpanSizeLookup(adapter, manager);
            manager.setSpanSizeLookup(lookup);
            adapter.setOnItemClickListener(new VedioAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int section, int position) {
                    if (!chooseMode){
                      /*  Intent intent = new Intent(getActivity(), PreViewActivity.class);
                        intent.putExtra("SRC-VED", listVedio.get(section).get(position));
                        startActivity(intent);*/

                        Intent intent =new Intent(Intent.ACTION_VIEW);
                        Uri uri= Uri.fromFile(new File(listVedio.get(section).get(position)));
                        Log.i("URI",""+uri);
                        intent.setDataAndType(uri,"video/*");
                        startActivity(intent);
                    }else {
                        if (vedio_staus[section][position]){
                            vedio_staus[section][position]=false;
                            deleteList.remove(listVedio.get(section).get(position));
                        }else {
                            vedio_staus[section][position]=true;
                            deleteList.add(listVedio.get(section).get(position));
                        }
                        adapter.notifyDataSetChanged();
                    }
                }
            });
            recyclerView.setLayoutManager(manager);
            recyclerView.setAdapter(adapter);
        }
    }

    private void updateStatus() {
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                vedio_staus[i][j]=false;
            }
        }
    }


    public void notifydata() {
        dismiss();
        showDialog("正在搜索...");
        SearcherThread threadVdeio = new SearcherThread(WifiP2pSocket.path, 2);
        threadVdeio.setVedioCallback(new VEDCallback() {

            @Override
            public void sortVedioBcak(ArrayList<ArrayList<String>> url) {
                listVedio = url;
                Log.i("VedioFragment",listVedio.size()+"size");
                handler.sendEmptyMessage(2);
            }

        });
        threadVdeio.start();
    }

    public void update() {
        notifydata();
    }

    public  void showDialog(String msg) {
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setIndeterminate(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(true);
        dialog.setMessage(msg);
        dialog.show();
    }

    public  void dismiss() {
        if (dialog!=null){
            dialog.dismiss();
        }
    }

}